package com.hanyi.mvc.contorller;

import com.hanyi.mvc.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
//@ResponseBody  定义到类上之后, 类中的所有方法都返回数据都被转为json
//@RestController  相当于@Controller + @ ResquestBody
public class RestfulContorller {

    // 根据id删除 delete请求
    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteUser(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        return "ok";
    }

    // 根据id查询
    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    @ResponseBody
    public User selectById(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        return new User("tom",12,"男");
    }


    // post 添加
    @ResponseBody
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public String save(@RequestBody User user){
        System.out.println("user = " + user);
        return "ok";
    }

    // 修改
    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    @ResponseBody
    public String postUpadte(@RequestBody User user){ // 修改
        System.out.println("user = " + user);
        return "ok";
    }

    // 根据条件查询
    @GetMapping("/user/{id}/sex/{name}")
    @ResponseBody
    public User selectByTiaoJian(@PathVariable("id") Integer id, @PathVariable("name") String name){
        System.out.println("id = " + id);
        System.out.println("name = " + name);
        return new User("天剑",12,"男");
    }
}
